import { Directive, ElementRef, Input } from '@angular/core';
import { Clipboard } from '@ionic-native/clipboard';
import { ToastController, NavController } from 'ionic-angular';

@Directive({
  selector: '[clipboard]' // Attribute selector
})
export class CopyDirective {
  @Input('clipboard') val: any;
  ele: any;
  constructor(
    private clipboard: Clipboard,
    private el: ElementRef,
    private toastCtrl: ToastController,
  ) {
    this.ele = el.nativeElement;
  }
  ngAfterViewInit() {
    this.el.nativeElement.onclick = (event) => {
      this.clipboard.copy(this.val);
      this.clipboard.paste().then(
        (resolve: string) => {
          this.Toast('复制成功!');
        },
        (reject: string) => {
          this.Toast('您的手机不支持该功能!');
        }
      )
    }
  }
  private Toast(text) {
    let toast = this.toastCtrl.create({
      message: text,
      duration: 1000,
      position: 'middle'
    })
    toast.present();
  }

}

@Directive({
  selector: '[login]'
})
export class LoginDirective {
  @Input('login') val: any;
  constructor(
    private el: ElementRef,
    public navCtrl: NavController,
  ) {

  }
  ngOnChanges() {
    this.el.nativeElement.onclick = () => {
      if (!this.val) {
        this.navCtrl.push("LoginPage");
      }
    }
  }
}
